package com.blb.ssm.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.ssm.controller.base.BaseController;
import com.blb.ssm.entity.Menu;
import com.blb.ssm.entity.Role;
import com.blb.ssm.entity.UserRole;
import com.blb.ssm.service.IMenuService;
import com.blb.ssm.service.IRoleService;
import com.blb.ssm.service.IUserRoleService;
import com.blb.ssm.utils.PageEntity;
import com.blb.ssm.utils.ResponseEntity;
import com.blb.ssm.utils.UUIDUtils;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huahao
 * @since 2019-01-15
 */
@Controller
@RequestMapping("/a/role")
public class RoleController extends BaseController {

	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private IMenuService menuService;
	
	@Autowired
	private IUserRoleService userRoleService;
	
	@RequiresPermissions("role:list")
	@RequestMapping("listPage")
	public String listPage(String id, Model model){
		List<Menu> menus = menuService.list(new QueryWrapper<Menu>().eq("pid", id).orderByAsc("seq"));
		model.addAttribute("menus", menus);
		return disPlay(ADMIN, "role", "role_list");
	}
	
	/**
	 * 分页数据
	 * @param page
	 * @param limit
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list")
	public PageEntity roleList(int page, int limit, Role role){
		Page<Role> page2 = new Page<Role>(page, limit);
		QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
		if (role!=null){
			if (!StringUtils.isEmpty(role.getRole())) queryWrapper.like("role", role.getRole());
		}
		return new PageEntity(roleService.page(page2, queryWrapper));
	}
	
	/**
	 * 保存
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public ResponseEntity addRole(Role role){
		Role role2 = roleService.getOne(new QueryWrapper<Role>().eq("role", role.getRole()));
		if (role2!=null) {
			return error("角色名已存在");
		}
		role.setId(UUIDUtils.getID());
		role.setCreateTime(new Date());
		roleService.save(role);
		return success();
	}
	
	/**
	 * 删除（支持批量删除）
	 * @param roles
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public ResponseEntity delete(@RequestBody ArrayList<Role> roles){
		try{
			List<String> list = new ArrayList<String>();
			for (Role role : roles) {
				if ("root".equals(role.getRole())) {
					throw new Exception("root角色不能被删除");
				}
				list.add(role.getId());				
			}
			roleService.removeByIds(list);
		} catch (Exception e) {
			e.printStackTrace();
			return error(e.getMessage());
		}
		return success();
	}
	
	/**
	 * 跳转修改界面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("updatePage")
	public String adminPage(String id, Model model){
		Role role = roleService.getById(id);
		model.addAttribute("role", role);
		return disPlay(ADMIN, "role", "role_update");
	}
	
	/**
	 * 修改
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public ResponseEntity updateRole(Role role){
		role.setUpdateTime(new Date());
		roleService.updateById(role);
		return success();
	}
	
	/**
	 * 角色列表
	 * @param page
	 * @param limit
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("roleList")
	public PageEntity List(String userId, Role role){
		List<UserRole> userRoles = userRoleService.list(new QueryWrapper<UserRole>().eq("user_id", userId));
		
		QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
		if (role!=null){
			if (!StringUtils.isEmpty(role.getRole())) queryWrapper.like("role", role.getRole());
		}
		List<Role> roles = roleService.list(queryWrapper);
		
		List<JSONObject> list = new ArrayList<JSONObject>();
		for (Role role2 : roles) {
			JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(role2));
			
			boolean rs = false;
			for (UserRole userRole : userRoles) {
				if (userRole.getRoleId().equals(role2.getId())) {
					rs = true;
				}
			}
			jsonObject.put("LAY_CHECKED", rs);
			list.add(jsonObject);
		}
		return new PageEntity(list.size(), list);
	}
				
	/**
	 * 设置权限
	 * @param id
	 * @param roles
	 * @return
	 */
	@ResponseBody
	@RequestMapping("setMenu")
	public ResponseEntity setMenu(String id, @RequestBody ArrayList<Menu> menus){
		roleService.setMenu(id, menus);
		return success();
	}
}

